package day0724API;

import java.util.HashMap;
import java.util.Objects;

public class Test7 {
    public static void main(String[] args) {
        HashMap<String, Person> pers=new HashMap<>();
        Person p1=new Person("张三");
        pers.put("zs1001", p1);

        //1.存元素
        Person p2=new Person("张三3");
        pers.put("zs1002",p2);
        System.out.println(pers);

        //2.取元素
        Person p3=pers.get("zs1001");
        System.out.println(p3);

        //3.根据key删除元素(返回被删除的元素)
        Person old=pers.remove("zs1001");
        System.out.println(pers);
        System.out.println(old);

        //4.求集合长度
        int s=pers.size();
        System.out.println(s);

        //5.判断是否包含key
        boolean rs=pers.containsKey("zs1001");
        System.out.println(rs);

        //6.判断是否包含value
        boolean rs2=pers.containsValue(new Person("张三3"));
        System.out.println(rs2);
        System.out.println(pers);

    }
}




class Person{
    String name;
    public Person(String name) {
        this.name = name;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(name, person.name);
    }

    public int hashCode() {
        return Objects.hash(name);
    }

    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}